package com.b2c.ftysys.sevice.timerjob;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.b2c.ftysys.domain.ExampleFactory;
import com.b2c.ftysys.domain.Order;
import com.b2c.ftysys.domain.OrderExample;
import com.b2c.ftysys.domain.PageResult;
import com.b2c.ftysys.sevice.OrderReviewService;

@Service
public class OrderJobWorker {

	@Autowired
	private OrderReviewService orderReviewService = null;

	/**
	 * 更新订单结束标示,每天晚上9点59分钟执行一次
	 */
	@Scheduled(cron = "0 59 21 ? * *")
	public void makeUpOrder() {
		OrderExample example = ExampleFactory.createOrderExample();
		example.createCriteria()
				.andOrderstatusNotEqualTo(Order.ORDER_DONE + "");
		PageResult page = new PageResult();
		page.setSqlParamObj(example);

		page = orderReviewService.getOrderList(page);
		// 每次执行100条记录
		page.setPageSize(100);
		page.setCurPage(1);
		do {
			for (Object obj : page.getPageList()) {
				if (obj instanceof Order) {
					Order order = (Order) obj;
					if (orderReviewService.checkOrderIsDone(order)) {
						orderReviewService.updateOrderStatus(order,
								Order.ORDER_DONE);
					}
				} else {
					continue;
				}
			}
			// 翻页来执行
			page.setCurPage(page.getCurPage() + 1);
			System.out.println("当前页:" + page.getCurPage() + " ——————>"
					+ page.getPageCnt());
		} while (page.getCurPage() < page.getPageCnt());
	}

}
